package com.ruoyi.system.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.HmzBorrowDetail;
import com.ruoyi.system.domain.HmzInventoryDetail;
import com.ruoyi.system.domain.HmzItem;
import com.ruoyi.system.service.IHmzBorrowDetailService;
import com.ruoyi.system.service.IHmzInventoryDetailService;
import com.ruoyi.system.service.IHmzItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HmzInventoryMaster;
import com.ruoyi.system.service.IHmzInventoryMasterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 盘点单主Controller
 * 
 * @author ruoyi
 * @date 2024-10-31
 */
@RestController
@RequestMapping("/system/HmzInventoryMaster")
public class HmzInventoryMasterController extends BaseController
{
    @Autowired
    private IHmzInventoryMasterService hmzInventoryMasterService;
    //    盘点单子表操作类
    @Autowired
    private IHmzInventoryDetailService hmzInventoryDetailService;
    //    托盘操作类
    @Autowired
    private IHmzItemService hmzItemService;
    /**
     * 查询盘点单主列表
     */
    @PreAuthorize("@ss.hasPermi('system:HmzInventoryMaster:list')")
    @GetMapping("/list")
    public TableDataInfo list(HmzInventoryMaster hmzInventoryMaster)
    {
        startPage();
        List<HmzInventoryMaster> list = hmzInventoryMasterService.selectHmzInventoryMasterList(hmzInventoryMaster);
        return getDataTable(list);
    }

    /**
     * 导出盘点单主列表
     */
    @PreAuthorize("@ss.hasPermi('system:HmzInventoryMaster:export')")
    @Log(title = "盘点单主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HmzInventoryMaster hmzInventoryMaster)
    {
        List<HmzInventoryMaster> list = hmzInventoryMasterService.selectHmzInventoryMasterList(hmzInventoryMaster);
        ExcelUtil<HmzInventoryMaster> util = new ExcelUtil<HmzInventoryMaster>(HmzInventoryMaster.class);
        util.exportExcel(response, list, "盘点单主数据");
    }

    /**
     * 获取盘点单主详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:HmzInventoryMaster:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hmzInventoryMasterService.selectHmzInventoryMasterById(id));
    }

    /**
     * 新增盘点单主
     */
    @PreAuthorize("@ss.hasPermi('system:HmzInventoryMaster:add')")
    @Log(title = "盘点单主", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody HmzInventoryMaster hmzInventoryMaster)
    {
        //        根据日期生成流水号 start
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Calendar calendar = Calendar.getInstance();
        String datePortion = sdf.format(calendar.getTime());
        System.out.println("生成的序列编号是: " + datePortion); // 输出序列编号
        hmzInventoryMaster.setPdNumber(datePortion);
//       根据日期生成流水号 end
//        插入盘点单主表 拿到主表id
        hmzInventoryMasterService.insertHmzInventoryMaster(hmzInventoryMaster);
        Long tableId = hmzInventoryMaster.getId();
        List<HmzInventoryDetail> hmzInventoryDetails = hmzInventoryMaster.getHmzInventoryDetails();
//       检测抛出异常
        HmzItem hmzItem;
        //       遍历将主表id 和 领用明细插入领用子表
        for (HmzInventoryDetail item : hmzInventoryDetails) {
            //        赋值主表id
            item.setMasterId(tableId);
//            根据托盘rfid获取托盘id
            hmzItem = hmzItemService.selectHmzItemByRfidCode(item.getRfidCode());
            Long tuoPanId =hmzItem.getId();
            System.out.println("托盘id: " + tuoPanId);
//           赋值托盘id
            item.setItemId(tuoPanId);
//        插入数据
            hmzInventoryDetailService.insertHmzInventoryDetail(item);
        }
        return success("操作成功");
    }

    /**
     * 修改盘点单主
     */
    @PreAuthorize("@ss.hasPermi('system:HmzInventoryMaster:edit')")
    @Log(title = "盘点单主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HmzInventoryMaster hmzInventoryMaster)
    {
        return toAjax(hmzInventoryMasterService.updateHmzInventoryMaster(hmzInventoryMaster));
    }

    /**
     * 删除盘点单主
     */
    @PreAuthorize("@ss.hasPermi('system:HmzInventoryMaster:remove')")
    @Log(title = "盘点单主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hmzInventoryMasterService.deleteHmzInventoryMasterByIds(ids));
    }
}
